/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rdb.sbean;

import com.rdb.entities.*;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Bich Tran Van
 */
@Stateless
public class UsersFacade extends AbstractFacade<Users> {
    @PersistenceContext(unitName = "ResultDeclarationBoard-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsersFacade() {
        super(Users.class);
    }
    
    
    public Users getUserlogin(String username, String password){
        String query = "SELECT u FROM Users u WHERE u.username = :username and u.password = :password";
        javax.persistence.Query q = getEntityManager().createQuery(query);
        q.setParameter("username", username);
        q.setParameter("password", password);
        
        if(q.getResultList().isEmpty()){
            return null;
        }
        return ((Users)q.getSingleResult());
    }
    
    public Users getUserByUsername(String username){
        String query = "SELECT u FROM Users u WHERE u.username = :username";
        javax.persistence.Query q = getEntityManager().createQuery(query);
        q.setParameter("username", username);
        
        if(q.getResultList().isEmpty()){
            return null;
        }
        return ((Users)q.getSingleResult());
    }
    
    public List<Users> getfaculty(){
        String query = "SELECT u FROM Users u WHERE u.groupId.groupId = 1";
        javax.persistence.Query q = getEntityManager().createQuery(query);
        
        if(q.getResultList().isEmpty()){
            return null;
        }
        return q.getResultList();
    }
    
    
}
